package com.gryffonx.utils;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;
import org.xml.sax.helpers.DefaultHandler;

import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;


/**
 * **************************************************************************
 * Application:
 * Author: Erik Stell
 * Date: Sep 3, 2004
 * Time: 2:34:16 PM
 * Process:
 * **************************************************************************
 */
public class XMLParser extends DefaultHandler {

    public static void main(String argv[]) {

        //Code to read from an XML File
        /**
         try {
         File fileToBeParsed = new File("src/com/gryffonx/xml/elf.xml");
         SAXReader reader = new SAXReader();
         Document xmlHelp = null;

         xmlHelp = reader.read(fileToBeParsed);
         int rolld20_2 = StaticDiceRoller.rolldSomething(36, 1);

         String targetId = new Integer(rolld20_2).toString();
         Object[] trainingClassArray = xmlHelp.selectNodes("//prefixElements/prefix[@prefix_id='1']/prefix_element[@prefix_element_id='"+targetId+"']").toArray();
         Node thisNode = (Node)trainingClassArray[0];

         System.out.println("Generated Number:" +targetId+ " and result:" +thisNode.getStringValue());



         } catch (Exception e) {
         e.printStackTrace();
         }
         **/

        //Code to write an aml file


        ArrayList valueArray = new ArrayList();
        valueArray.add("value1");
        valueArray.add("value2");
        valueArray.add("value3");
        valueArray.add("value4");
        valueArray.add("value5");
        valueArray.add("value6");

        Document document = DocumentHelper.createDocument();
        Element root = document.addElement("root");
        for (Object aValueArray : valueArray) {
            String s = aValueArray.toString();
            root.addElement(s).addText(s);
        }


        try {
            // lets write to a file
            OutputFormat format = OutputFormat.createPrettyPrint();
            XMLWriter writer = new XMLWriter(new FileWriter("output.xml"), format);
            writer.write(document);
            writer.close();

        } catch (IOException e) {
            e.printStackTrace();
        }


    }
}
